﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;

using ZX.SYS.Library;
using OpenExpressApp;
using ZX.Web.UI.Controls;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace ZX.WebApp.DesignForm
{
    public partial class AlterForm : BasePage
    {
        /// <summary>
        /// 模板名
        /// </summary>
        public string TemplateName
        {
            get { return "autoUI"; }
        }
        /// <summary>
        /// 表单ID
        /// </summary>
        public string FormID
        {
            get;
            set;
        }
        /// <summary>
        /// 表单名
        /// </summary>
        public string FormName
        {
            get;
            set;
        }
        /// <summary>
        /// 主表名
        /// </summary>
        public string MasterTableName
        {
            get;
            set;
        }

        /// <summary>
        /// 主表JSON
        /// </summary>
        public string MasterJson
        {
            get;
            set;
        }
        /// <summary>
        /// 明细表JSON
        /// </summary>
        public string DetailsJson
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public string FieldItemsJson
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            //DateTime dt = DateTime.Now;
            string strAction = this.GetStringFromQueryString("action", "");
            string strFormID = this.GetStringFromQueryString("formid", "");
            string strEntity = this.GetStringFromQueryString("entity", "");
            string strTable = this.GetStringFromQueryString("table", "");
            string strFormName = this.GetStringFromQueryString("formname", "");
            string strJson = this.GetStringFromQueryString("json", "").Replace("[@@]", "\\");

            switch (strAction)
            {
                case "CreateForm":
                    //CreateForm(strFormName,strTable, strJson.Replace("[A]", "&"));
                    break;
                case "EditForm":
                    //InsertOrUpdate(strEntity, strJson.Replace("[A]", "&"), strAction);
                    EditForm(strFormID);
                    break;
                default:
                    break;

            }


            if (!this.IsPostBack)
            {
                //GetTableNames();
                //cblcols.Attributes.Add("onload ", "setTableName(); ");
            }
        }
        private void EditForm(string formID)
        {
            if (!string.IsNullOrEmpty(formID))
            {
                SysCustomDefineForm form = new SysCustomDefineForm();
                Guid _FormID = new Guid(formID);
                form = SysCustomDefineForm.Get(_FormID);

                FormID = form.FormID.ToString();
                FormName = form.FormName;
                MasterTableName = form.MasterTableName;
                DetailsJson = form.DetailsJson;
                // IList<SysFieldItem> items = form.FieldItems.ToList<SysFieldItem>();
                //FieldItemsJson = ListToJson(items);
                CKEditorDesignForm.Text = form.HtmlString;
            }
        }
        public string ObjectListJSON(ArrayList Dic)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(Dic);
        }
    }
}